package com.thrc.borrow.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.SystemBill;
import com.thrc.borrow.service.SystemBillService;
import com.thrc.borrow.utils.Container;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.SystemBillVo;

@Controller
@RequestMapping("/sysbill")
public class SystemBillController{
	@Autowired
	private SystemBillService systemBillService;
	
	@RequestMapping("/goJsp")
	public String goUserJsp(HttpServletResponse response, HttpServletRequest request) {
		return "gold/sysbillList";
	}
	
	@RequestMapping(value = "/getTypeJson")
	public void getTypeJson(HttpServletResponse response) {
		JSONArray retJson = new JSONArray();

		JSONObject json1 = new JSONObject();
		json1.put(ReturnCode.RETURN_DATA, "入账");

		JSONObject json2 = new JSONObject();
		json2.put(ReturnCode.RETURN_DATA, "出账");
		
		retJson.add(json1);
		retJson.add(json2);

		ResponseUtil.print(response, retJson);
	}
	
	@RequestMapping(value = "/getTypeInfoJson")
	public void getTypeInfoJson(HttpServletResponse response) {
		JSONArray retJson = new JSONArray();

		JSONObject json1 = new JSONObject();
		json1.put(ReturnCode.RETURN_DATA, "放款");

		JSONObject json2 = new JSONObject();
		json2.put(ReturnCode.RETURN_DATA, "还款");
		
		JSONObject json3 = new JSONObject();
		json3.put(ReturnCode.RETURN_DATA, "奖励");
		
		JSONObject json4 = new JSONObject();
		json4.put(ReturnCode.RETURN_DATA, "手续费");
		
		retJson.add(json1);
		retJson.add(json2);
		retJson.add(json3);
		retJson.add(json4);

		ResponseUtil.print(response, retJson);
	}
	
	@RequestMapping("/getById")
	public void selectById(HttpServletResponse response, HttpServletRequest request, SystemBill t) {
		ResponseUtil.print(response, systemBillService.selectById(t));
	}
	
	@RequestMapping("/selectAll")
	public void selectAll(HttpServletResponse response, HttpServletRequest request, SystemBill t) {
		try {
			List<SystemBillVo> list = systemBillService.selectAll(t);
			ResponseUtil.print(response, list);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/selectByPage")
	public void selectByPage(HttpServletResponse response, HttpServletRequest request, SystemBill t) {
		JSONObject retJson = new JSONObject();
		try {
			int totleCount = systemBillService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<SystemBillVo> list = systemBillService.selectByPage(t);
			retJson.put("data", list);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/update")
	public void update(HttpServletResponse response, HttpServletRequest request, SystemBill t) {
		try {
			systemBillService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, HttpServletRequest request, SystemBill t) {
		try {
			systemBillService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/insert")
	public void insert(HttpServletResponse response, HttpServletRequest request, SystemBill t) {
		try {
			t.setCreateTime(new Date());
			systemBillService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

}
